/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.values;

import java.io.InputStream;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class defines values of the <i>in_channel</i> type.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class InChannel implements ToValue {

    /** Underlying stream. */
    private final InputStream stream;

    /**
     * Constructs an input channel.
     * @param s underlying stream - should not be <tt>null</tt>
     */
    public InChannel(final InputStream s) {
        assert s != null : "null s";
        this.stream = s;
    } // end constructor(InputStream)

    /**
     * {@inheritDoc}
     */
    @Override
    public Value toValue() {
        final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                             Custom.CHANNEL_OPS);
        res.setCustom(new Channel(this.stream));
        return Value.createFromBlock(res);
    } // end method 'toValue()'

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return this.stream.hashCode();
    } // end method 'hashCode()'

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof InChannel) {
            final InChannel that = (InChannel) obj;
            return this.stream == that.stream;
        } else {
            return false;
        } // end if/else
    } // end method 'equals(Object)'

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return this.stream.toString();
    } // end method 'toString()'

} // end class 'InChannel'
